﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.BasicInfo
{
    public partial class CourseInfo : System.Web.UI.Page
    {
        static int teacherid = 0;
        static int subjectid = 0;
        static int courseid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myCourse = from c in db.CourseInfo
                                    select c;
                    this.Store2.DataSource = myCourse;
                    this.Store2.DataBind();
                }
            }
        }

        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                subjectid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var mySubject = from s in db.SubjectInfo
                              where s.SubjectID == subjectid
                              select s.SubjectName;
                foreach (var subject in mySubject)
                {
                    DropDownField1.Text = subject.ToString();
                }
                var myCourse = from c in db.CourseInfo
                              where c.SubjectID == subjectid
                              select c;
                this.Store2.DataSource = myCourse;
                this.Store2.DataBind();
            }
            subjectid = 0;
        }

        protected void Button_2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                teacherid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myTeacher = from t in db.TeacherInfo
                              where t.TeacherID == teacherid
                              select t.TeacherName;
                foreach (var teacher in myTeacher)
                {
                    DropDownField2.Text = teacher.ToString();
                }
                var myCourse = from c in db.CourseInfo
                               where c.TeacherID==teacherid
                               select c;
                this.Store2.DataSource = myCourse;
                this.Store2.DataBind();
            }
            teacherid = 0;
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            CourseCode.Text = "";
            CourseName.Text = "";
            DropDownField3.Text = "请选择";
            DropDownField4.Text = "请选择";
            subjectid = 0;
            courseid = 0;
            this.FormWindow.Visible = true;
            this.FormWindow.Show();
            Button7.Show();
            Button8.Hide();
        }

        protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    courseid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newcourse = from c in db.CourseInfo
                                   where c.CourseID == courseid
                                   select c;
                    ExamAnalysis.Model.CourseInfo newc = newcourse.First();
                    var subject = from s in db.SubjectInfo
                                 where s.SubjectID == newc.SubjectID
                                 select s.SubjectName;
                    var teacher = from t in db.TeacherInfo
                                  where t.TeacherID == newc.TeacherID
                                  select t.TeacherName;
                    subjectid =  Convert.ToInt32(newc.SubjectID);
                    teacherid = newc.TeacherID;
                    CourseCode.Text = newc.CourseCode;
                    CourseName.Text = newc.CourseName;
                    DropDownField3.Text = subject.First().ToString();
                    DropDownField4.Text = teacher.First().ToString();
                }
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button7.Hide();
                Button8.Show();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个课程").Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    courseid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newcourse = from c in db.CourseInfo
                                    where c.CourseID == courseid
                                    select c;
                    ExamAnalysis.Model.CourseInfo newc = newcourse.First();
                    db.CourseInfo.DeleteOnSubmit(newc);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("CourseInfo.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个课程").Show();
            }
        }

        protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                subjectid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var mySubject = from s in db.SubjectInfo
                              where s.SubjectID == subjectid
                              select s.SubjectName;
                foreach (var subject in mySubject)
                {
                    DropDownField3.Text = subject.ToString();
                }
            }
        }

        protected void Button_5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel4.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                teacherid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myTeacher = from t in db.TeacherInfo
                              where t.TeacherID == teacherid
                              select t.TeacherName;
                foreach (var teacher in myTeacher)
                {
                    DropDownField4.Text = teacher.ToString();
                }
            }
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (DropDownField3.Text.ToString().Equals("请选择"))
            {
                X.Msg.Alert("提示信息", String.Format("请选择一个科目")).Show();
            }
            else
            {
                if (DropDownField4.Text.ToString().Equals("请选择"))
                {
                    X.Msg.Alert("提示信息", String.Format("请选择一个教师")).Show();
                }
                else
                {
                    ExamAnalysis.Model.CourseInfo newc = new ExamAnalysis.Model.CourseInfo();
                    newc.CourseCode = CourseCode.Text;
                    newc.CourseName = CourseName.Text;;
                    newc.SubjectID = subjectid;
                    newc.TeacherID = teacherid;
                    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                    {
                        db.CourseInfo.InsertOnSubmit(newc);
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("成功添加课程")).Show();
                        subjectid = 0;
                        teacherid = 0;
                    }
                    Response.Redirect("CourseInfo.aspx");
                }
            }
        }

        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newCourse = from c in db.CourseInfo
                               where c.CourseID == courseid
                               select c;
                ExamAnalysis.Model.CourseInfo newc = newCourse.First();
                newc.CourseCode = CourseCode.Text;
                newc.CourseName = CourseName.Text;
                newc.SubjectID = subjectid;
                newc.TeacherID = teacherid;
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功修改课程信息")).Show();
                subjectid = 0;
                teacherid = 0;
                courseid = 0;
            }
            Response.Redirect("CourseInfo.aspx");
        }
    }
}